4 // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
6 // The use and distribution terms for this software are contained in the file
7 // named license.txt, which can be found in the root of this distribution.
8 // By using this software in any fashion, you are agreeing to be bound by the
9 // terms of this license.
11 // You must not remove this notice, or any other, from this software.
16 namespace Microsoft
.JScript
{
18 internal sealed class JSKeyword
{
19 private JSKeyword next
;
20 private JSToken token
;
24 private JSKeyword(JSToken token
, string name
){
28 this.length
= this.name
.Length
;
31 private JSKeyword(JSToken token
, string name
, JSKeyword next
){
35 this.length
= this.name
.Length
;
38 internal static string CanBeIdentifier(JSToken keyword
){
40 case JSToken
.Abstract
: return "abstract";
41 case JSToken
.Assert
: return "assert";
42 case JSToken
.Boolean
: return "boolean";
43 case JSToken
.Byte
: return "byte";
44 case JSToken
.Char
: return "char";
45 case JSToken
.Decimal
: return "decimal";
46 case JSToken
.Double
: return "double";
47 case JSToken
.Ensure
: return "ensure";
48 case JSToken
.Enum
: return "enum";
49 case JSToken
.Event
: return "event";
50 case JSToken
.Final
: return "final";
51 case JSToken
.Float
: return "float";
52 case JSToken
.Get
: return "get";
53 case JSToken
.Goto
: return "goto";
54 case JSToken
.Implements
: return "implements";
55 case JSToken
.Int
: return "int";
56 case JSToken
.Interface
: return "interface";
57 case JSToken
.Internal
: return "internal";
58 case JSToken
.Invariant
: return "invariant";
59 case JSToken
.Long
: return "long";
60 case JSToken
.Namespace
: return "namespace";
61 case JSToken
.Native
: return "native";
62 case JSToken
.Package
: return "package";
63 case JSToken
.Private
: return "private";
64 case JSToken
.Protected
: return "protected";
65 case JSToken
.Public
: return "public";
66 case JSToken
.Require
: return "require";
67 case JSToken
.Sbyte
: return "sbyte";
68 case JSToken
.Set
: return "set";
69 case JSToken
.Short
: return "short";
70 case JSToken
.Static
: return "static";
71 case JSToken
.Synchronized
: return "synchronized";
72 case JSToken
.Throws
: return "throws";
73 case JSToken
.Transient
: return "transient";
74 case JSToken
.Void
: return "void";
75 case JSToken
.Volatile
: return "volatile";
76 case JSToken
.Uint
: return "uint";
77 case JSToken
.Ulong
: return "ulong";
78 case JSToken
.Ushort
: return "ushort";
79 case JSToken
.Use
: return "use";
84 internal JSToken
GetKeyword(Context token
, int length
){
85 JSKeyword keyword
= this;
88 while (null != keyword
){
89 if (length
== keyword
.length
){
90 // we know the first char has to match
91 for (int i
= 1, j
= token
.startPos
+ 1; i
< length
; i
++, j
++){
92 char ch1
= keyword
.name
[i
];
93 char ch2
= token
.source_string
[j
];
97 return JSToken
.Identifier
;
99 keyword
= keyword
.next
;
103 return keyword
.token
;
104 }else if (length
< keyword
.length
)
105 return JSToken
.Identifier
;
107 keyword
= keyword
.next
;
109 return JSToken
.Identifier
;
112 internal static JSKeyword
[] InitKeywords(){
113 JSKeyword
[] keywords
= new JSKeyword
[26];
116 keyword
= new JSKeyword(JSToken
.Abstract
, "abstract");
117 keyword
= new JSKeyword(JSToken
.Assert
, "assert", keyword
);
118 keywords
['a' - 'a'] = keyword
;
120 keyword
= new JSKeyword(JSToken
.Boolean
, "boolean");
121 keyword
= new JSKeyword(JSToken
.Break
, "break", keyword
);
122 keyword
= new JSKeyword(JSToken
.Byte
, "byte", keyword
);
123 keywords
['b' - 'a'] = keyword
;
125 keyword
= new JSKeyword(JSToken
.Continue
, "continue");
126 keyword
= new JSKeyword(JSToken
.Const
, "const", keyword
);
127 keyword
= new JSKeyword(JSToken
.Class
, "class", keyword
);
128 keyword
= new JSKeyword(JSToken
.Catch
, "catch", keyword
);
129 keyword
= new JSKeyword(JSToken
.Char
, "char", keyword
);
130 keyword
= new JSKeyword(JSToken
.Case
, "case", keyword
);
131 keywords
['c' - 'a'] = keyword
;
133 keyword
= new JSKeyword(JSToken
.Debugger
, "debugger");
134 keyword
= new JSKeyword(JSToken
.Default
, "default", keyword
);
135 keyword
= new JSKeyword(JSToken
.Double
, "double", keyword
);
136 keyword
= new JSKeyword(JSToken
.Delete
, "delete", keyword
);
137 keyword
= new JSKeyword(JSToken
.Do
, "do", keyword
);
138 keywords
['d' - 'a'] = keyword
;
140 keyword
= new JSKeyword(JSToken
.Extends
, "extends");
141 keyword
= new JSKeyword(JSToken
.Export
, "export", keyword
);
142 keyword
= new JSKeyword(JSToken
.Ensure
, "ensure", keyword
);
143 keyword
= new JSKeyword(JSToken
.Event
, "event", keyword
);
144 keyword
= new JSKeyword(JSToken
.Enum
, "enum", keyword
);
145 keyword
= new JSKeyword(JSToken
.Else
, "else", keyword
);
146 keywords
['e' - 'a'] = keyword
;
148 keyword
= new JSKeyword(JSToken
.Function
, "function");
149 keyword
= new JSKeyword(JSToken
.Finally
, "finally", keyword
);
150 keyword
= new JSKeyword(JSToken
.Float
, "float", keyword
);
151 keyword
= new JSKeyword(JSToken
.Final
, "final", keyword
);
152 keyword
= new JSKeyword(JSToken
.False
, "false", keyword
);
153 keyword
= new JSKeyword(JSToken
.For
, "for", keyword
);
154 keywords
['f' - 'a'] = keyword
;
156 keyword
= new JSKeyword(JSToken
.Goto
, "goto");
157 keyword
= new JSKeyword(JSToken
.Get
, "get", keyword
);
158 keywords
['g' - 'a'] = keyword
;
160 keyword
= new JSKeyword(JSToken
.Instanceof
, "instanceof");
161 keyword
= new JSKeyword(JSToken
.Implements
, "implements", keyword
);
162 keyword
= new JSKeyword(JSToken
.Invariant
, "invariant", keyword
);
163 keyword
= new JSKeyword(JSToken
.Interface
, "interface", keyword
);
164 keyword
= new JSKeyword(JSToken
.Internal
, "internal", keyword
);
165 keyword
= new JSKeyword(JSToken
.Import
, "import", keyword
);
166 keyword
= new JSKeyword(JSToken
.Int
, "int", keyword
);
167 keyword
= new JSKeyword(JSToken
.In
, "in", keyword
);
168 keyword
= new JSKeyword(JSToken
.If
, "if", keyword
);
169 keywords
['i' - 'a'] = keyword
;
171 keyword
= new JSKeyword(JSToken
.Long
, "long");
172 keywords
['l' - 'a'] = keyword
;
174 keyword
= new JSKeyword(JSToken
.Namespace
, "namespace");
175 keyword
= new JSKeyword(JSToken
.Native
, "native", keyword
);
176 keyword
= new JSKeyword(JSToken
.Null
, "null", keyword
);
177 keyword
= new JSKeyword(JSToken
.New
, "new", keyword
);
178 keywords
['n' - 'a'] = keyword
;
180 keyword
= new JSKeyword(JSToken
.Protected
, "protected");
181 keyword
= new JSKeyword(JSToken
.Private
, "private", keyword
);
182 keyword
= new JSKeyword(JSToken
.Package
, "package", keyword
);
183 keyword
= new JSKeyword(JSToken
.Public
, "public", keyword
);
184 keywords
['p' - 'a'] = keyword
;
186 keyword
= new JSKeyword(JSToken
.Require
, "require");
187 keyword
= new JSKeyword(JSToken
.Return
, "return", keyword
);
188 keywords
['r' - 'a'] = keyword
;
190 keyword
= new JSKeyword(JSToken
.Synchronized
, "synchronized");
191 keyword
= new JSKeyword(JSToken
.Switch
, "switch", keyword
);
192 keyword
= new JSKeyword(JSToken
.Static
, "static", keyword
);
193 keyword
= new JSKeyword(JSToken
.Super
, "super", keyword
);
194 keyword
= new JSKeyword(JSToken
.Short
, "short", keyword
);
195 keyword
= new JSKeyword(JSToken
.Set
, "set", keyword
);
196 keywords
['s' - 'a'] = keyword
;
198 keyword
= new JSKeyword(JSToken
.Transient
, "transient");
199 keyword
= new JSKeyword(JSToken
.Typeof
, "typeof", keyword
);
200 keyword
= new JSKeyword(JSToken
.Throws
, "throws", keyword
);
201 keyword
= new JSKeyword(JSToken
.Throw
, "throw", keyword
);
202 keyword
= new JSKeyword(JSToken
.True
, "true", keyword
);
203 keyword
= new JSKeyword(JSToken
.This
, "this", keyword
);
204 keyword
= new JSKeyword(JSToken
.Try
, "try", keyword
);
205 keywords
['t' - 'a'] = keyword
;
207 keyword
= new JSKeyword(JSToken
.Volatile
, "volatile");
208 keyword
= new JSKeyword(JSToken
.Void
, "void", keyword
);
209 keyword
= new JSKeyword(JSToken
.Var
, "var", keyword
);
210 keywords
['v' - 'a'] = keyword
;
212 keyword
= new JSKeyword(JSToken
.Use
, "use");
213 keywords
['u' - 'a'] = keyword
;
215 keyword
= new JSKeyword(JSToken
.While
, "while");
216 keyword
= new JSKeyword(JSToken
.With
, "with", keyword
);
217 keywords
['w' - 'a'] = keyword
;